package com.ruoyi.report_yj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_yj.domain.SnjjqdjlbBsDetail;
import com.ruoyi.report_yj.domain.vo.SnjjqdjlbBsDetailVo;
import com.ruoyi.report_yj.mapper.SnjjqdjlbBsDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_yj.domain.bo.SnjjqdjlbBsInfoBo;
import com.ruoyi.report_yj.domain.vo.SnjjqdjlbBsInfoVo;
import com.ruoyi.report_yj.domain.SnjjqdjlbBsInfo;
import com.ruoyi.report_yj.mapper.SnjjqdjlbBsInfoMapper;
import com.ruoyi.report_yj.service.ISnjjqdjlbBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 水泥净浆强度试验检测记录Service业务层处理
 *
 * @author llb
 * @date 2024-06-12
 */
@RequiredArgsConstructor
@Service
public class SnjjqdjlbBsInfoServiceImpl implements ISnjjqdjlbBsInfoService {

    private final SnjjqdjlbBsInfoMapper baseMapper;
    private final SnjjqdjlbBsDetailMapper snjjqdjlbBsDetailMapper;

    /**
     * 查询水泥净浆强度试验检测记录
     */
    @Override
    @FIllBasicInfo
    public SnjjqdjlbBsInfoVo queryById(Long excelId){
        SnjjqdjlbBsInfoVo snjjqdjlbBsInfoVo = baseMapper.selectVoById(excelId);
        List<SnjjqdjlbBsDetailVo> snjjqdjlbBsDetailList = snjjqdjlbBsDetailMapper.selectVoList(new LambdaQueryWrapper<SnjjqdjlbBsDetail>().eq(SnjjqdjlbBsDetail::getExcelId, excelId));
        snjjqdjlbBsInfoVo.setSnjjqdjlbBsDetailList(snjjqdjlbBsDetailList);
        return snjjqdjlbBsInfoVo;
    }

    /**
     * 查询水泥净浆强度试验检测记录列表
     */
    @Override
    public TableDataInfo<SnjjqdjlbBsInfoVo> queryPageList(SnjjqdjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SnjjqdjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<SnjjqdjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水泥净浆强度试验检测记录列表
     */
    @Override
    public List<SnjjqdjlbBsInfoVo> queryList(SnjjqdjlbBsInfoBo bo) {
        LambdaQueryWrapper<SnjjqdjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SnjjqdjlbBsInfo> buildQueryWrapper(SnjjqdjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SnjjqdjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, SnjjqdjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, SnjjqdjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), SnjjqdjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), SnjjqdjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), SnjjqdjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), SnjjqdjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), SnjjqdjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), SnjjqdjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), SnjjqdjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), SnjjqdjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), SnjjqdjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), SnjjqdjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), SnjjqdjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), SnjjqdjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), SnjjqdjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), SnjjqdjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), SnjjqdjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShejqd()), SnjjqdjlbBsInfo::getShejqd, bo.getShejqd());
        lqw.eq(StringUtils.isNotBlank(bo.getShijcc()), SnjjqdjlbBsInfo::getShijcc, bo.getShijcc());
        lqw.eq(StringUtils.isNotBlank(bo.getLiudd()), SnjjqdjlbBsInfo::getLiudd, bo.getLiudd());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), SnjjqdjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), SnjjqdjlbBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), SnjjqdjlbBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), SnjjqdjlbBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), SnjjqdjlbBsInfo::getRiq, bo.getRiq());
        return lqw;
    }

    /**
     * 新增水泥净浆强度试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(SnjjqdjlbBsInfoBo bo) {
        SnjjqdjlbBsInfo add = BeanUtil.toBean(bo, SnjjqdjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<SnjjqdjlbBsDetail> details = new ArrayList<>(9);
            for (int i=1;i<=9;i++ ){
                details.add(new SnjjqdjlbBsDetail(bo.getExcelId()));
            }
            snjjqdjlbBsDetailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改水泥净浆强度试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(SnjjqdjlbBsInfoBo bo) {
        SnjjqdjlbBsInfo update = BeanUtil.toBean(bo, SnjjqdjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            snjjqdjlbBsDetailMapper.updateBatchById(BeanUtil.copyToList(bo.getSnjjqdjlbBsDetailList(), SnjjqdjlbBsDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SnjjqdjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水泥净浆强度试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
